package com.surface.hao.frame;

import com.surface.hao.component.ScrollPanel;
import com.surface.hao.util.ComponentUtils;
import com.surface.hao.util.FileUtils;
import lombok.extern.log4j.Log4j;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description //TODO
 * @Date 2019/12/29
 * @Author songhao
 **/
@Log4j
public class FileMergePanel {

    static String inputDir = null;
    static String outputDir = null;
    static ScrollPanel tipArea = null;

    static JTextField dir1 = null;
    static JTextField dir2 = null;

    public static JPanel getJpanel(String titleName) {
        JPanel panel = new JPanel();
        //设置布局方式和大小
        panel.setLayout(new BorderLayout(0, 0));
        panel.setSize(800, 600);

        JLabel titleLabel = new JLabel(titleName,JLabel.CENTER);
        titleLabel.setBorder(new EmptyBorder(10, 10, 25, 10));
        titleLabel.setFont(new Font("宋体", Font.BOLD, 24));

        //第一个Panel
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new BorderLayout(0, 0));
        panel.add(titlePanel, BorderLayout.NORTH);

        //添加标题信息
        titlePanel.add(titleLabel, BorderLayout.NORTH);

        // 时间戳转换为日期这个Panel
        JPanel timeToDatePanel = getDirPanel();
        titlePanel.add(timeToDatePanel, BorderLayout.CENTER);

        // 日期字符串转换为时间戳Panel
        JPanel dateToTimePanel = getWordPanel();
        titlePanel.add(dateToTimePanel, BorderLayout.SOUTH);

        //第二个Panel
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout(0, 0));
        panel.add(centerPanel, BorderLayout.CENTER);

        tipArea = new ScrollPanel();
        tipArea.setPreferredSize(new Dimension(200, 350));
        centerPanel.add(tipArea, BorderLayout.CENTER);
        return panel;


        /*JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout(0, 0));
        panel.setSize(800, 600);

        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new BorderLayout(0, 0));
        panel.add(titlePanel, BorderLayout.NORTH);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout(0, 0));
        panel.add(centerPanel, BorderLayout.CENTER);

        JPanel panel_1 = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        panel_1.setBorder(new TitledBorder(UIManager
                .getBorder("TitledBorder.border"), "目标目录",
                TitledBorder.LEADING, TitledBorder.TOP, null,
                new Color(0, 0, 0)));
        titlePanel.add(panel_1, BorderLayout.CENTER);

        JTextField dir2 = new JTextField();
        dir2.setToolTipText("\u76EE\u6807\u76EE\u5F55\u5730\u5740\u680F\u3002");
        dir2.setText("");
        dir2.setFont(new Font("宋体", Font.BOLD, 24));
        dir2.setPreferredSize(new Dimension(600, 40));
        panel_1.add(dir2);

        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        panel_2.setBorder(new TitledBorder(UIManager
                .getBorder("TitledBorder.border"), "源目录",
                TitledBorder.LEADING, TitledBorder.TOP, null,
                new Color(0, 0, 0)));
        titlePanel.add(panel_2, BorderLayout.NORTH);

        JTextField dir1 = new JTextField();
        dir1.setText("");
        dir1.setFont(new Font("宋体", Font.BOLD, 24));
        dir1.setPreferredSize(new Dimension(600, 40));
        panel_2.add(dir1);

        JButton button_1 = new JButton("开始合并");
        button_1.setPreferredSize(new Dimension(100, 40));
        panel_1.add(button_1);

        //设置button监控
        button_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String inputDir = dir1.getText();
                String outputDir = dir2.getText();

                //对两个文件进行合并
                merge(inputDir,outputDir,jta);
                log.info("源文件:[" + inputDir + "],目标文件:[" + outputDir + "]");
            }
        });

        jta = new WordSearchPanel.MyTextArea();
        jta.setPreferredSize(new Dimension(200, 350));
        centerPanel.add(jta, BorderLayout.NORTH);
        return panel;*/
    }

    private static JPanel getDirPanel() {
        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        panel_2.setBorder(new TitledBorder(UIManager
                .getBorder("TitledBorder.border"), "源目录",
                TitledBorder.LEADING, TitledBorder.TOP, null,
                new Color(0, 0, 0)));

        dir1 = new JTextField();
        dir1.setText("");
        dir1.setFont(new Font("宋体", Font.BOLD, 24));
        dir1.setPreferredSize(new Dimension(600, 40));
        panel_2.add(dir1);

        return panel_2;
    }

    /**
     * 获取目录的Panel
     * @return
     */
    public static JPanel getWordPanel(){
        JPanel panel_1 = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        panel_1.setBorder(new TitledBorder(UIManager
                .getBorder("TitledBorder.border"), "目标文件",
                TitledBorder.LEADING, TitledBorder.TOP, null,
                new Color(0, 0, 0)));

        dir2 = new JTextField();
        dir2.setText("");
        dir2.setFont(new Font("宋体", Font.BOLD, 24));
        dir2.setPreferredSize(new Dimension(600, 40));
        panel_1.add(dir2);

        JButton button_1 = new JButton("开始合并");
        button_1.setPreferredSize(new Dimension(100, 40));
        panel_1.add(button_1);
        //设置button监控
        button_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                inputDir = dir1.getText();
                outputDir = dir2.getText();

                log.info("源文件:[" + inputDir + "],目标文件:[" + outputDir + "]");
                // 新启动一个线程进行检索
                new Thread(() -> merge(inputDir, outputDir, tipArea)).start();
            }
        });
        return panel_1;
    }

    /**
     * 对文件进行合并
     * @param inputDir
     * @param outputDir
     * @param tipArea
     */
    public static void merge(String inputDir, String outputDir, ScrollPanel tipArea) {
        List<String> allChildPaths = new ArrayList<String>();
        FileUtils.getAllChildPaths(inputDir, allChildPaths,true);

        for (String childPath : allChildPaths) {
            log.info("正在读取文件:[" + childPath + "]");
            mergeFile(childPath,outputDir,tipArea);
        }
    }

    /**
     * 合并文件
     * @param childPath
     * @param outputDir
     * @param tipArea
     */
    private static void mergeFile(String childPath, String outputDir, ScrollPanel tipArea) {
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try{
            reader = FileUtils.getReader(childPath);
            writer = FileUtils.getWriter(outputDir,true);
            String tempLine = null;
            int lineIndex = 0;
            while((tempLine = reader.readLine()) != null){
                lineIndex ++;
                writer.write(tempLine);
                writer.newLine();
            }
            ComponentUtils.setTips(tipArea,"源文件" + childPath + "的行数:[" + lineIndex + "]已写入到目标文件");
        }catch (Exception e){
            log.info("文件合并失败:" + e.getMessage());
        }finally {
            FileUtils.closeReader(reader);
            FileUtils.closeWrite(writer);
        }
    }
}
